package com.lc.afbx.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.activity.LoginActivity;
import com.lc.afbx.conn.GetApiLiPeiSearch;
import com.lc.afbx.conn.PostApiClaims;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.activity.AppV4Fragment;

import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.R;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilNetWord;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/12.
 */
public class LiPeiApplyFragment extends AppV4Fragment implements View.OnClickListener {

    @BoundView(R.id.fragment_lipeishenqing_submit_bt) private Button fragment_lipeishenqing_submit_bt;

    @BoundView(R.id.fragment_lipeishenqing_address_et) private EditText fragment_lipeishenqing_address_et;
    @BoundView(R.id.fragment_lipeishenqing_idnum_et) private EditText fragment_lipeishenqing_idnum_et;
    @BoundView(R.id.fragment_lipeishenqing_phone_et) private EditText fragment_lipeishenqing_phone_et;
    @BoundView(R.id.fragment_lipeishenqing_people_et) private EditText fragment_lipeishenqing_people_et;
    @BoundView(R.id.fragment_lipeishenqing_carcode_et) private EditText fragment_lipeishenqing_carcode_et;
    @BoundView(R.id.fragment_lipeishenqing_bankNum_et) private EditText fragment_lipeishenqing_bankNum_et;
    @BoundView(R.id.fragment_lipeishenqing_bankName_et) private EditText fragment_lipeishenqing_bankName_et;
    @BoundView(R.id.fragment_lipeishenqing_apply_type_tv) private TextView fragment_lipeishenqing_apply_type_tv;
    @BoundView(R.id.fragment_lipeishenqing_conpany_tv) private TextView fragment_lipeishenqing_conpany_tv;

    @BoundView(R.id.fragment_lipeishenqing_address_rl) private RelativeLayout fragment_lipeishenqing_address_rl;
    @BoundView(R.id.fragment_lipeishenqing_carcode_rl) private RelativeLayout fragment_lipeishenqing_carcode_rl;

    private ArrayList<String> tpyeList = new ArrayList<>();
    private List<String> companyList = new ArrayList<>();
    private List<GetApiLiPeiSearch.Data> mDataList = new ArrayList<>();


    //类型联动公司接口
    private GetApiLiPeiSearch getApiLiPeiSearch = new GetApiLiPeiSearch(new AsyCallBack<GetApiLiPeiSearch.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiLiPeiSearch.Info info) throws Exception {

            mDataList.clear();

            companyList.clear();

            mDataList.addAll(info.dataList);

            for (int i = 0; i < mDataList.size(); i++) {
                companyList.add(mDataList.get(i).title);
            }

            fragment_lipeishenqing_conpany_tv.setText(mDataList.get(0).title);
            fragment_lipeishenqing_conpany_tv.setTag(mDataList.get(0).id);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(getActivity(), toast);

        }
    });

    //提交的接口
    private PostApiClaims postApiClaims = new PostApiClaims(new AsyCallBack<PostApiClaims.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiClaims.Info info) throws Exception {

            UtilToast.show(getActivity(), toast);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(getActivity(), toast);

        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View view = BoundViewHelper.boundView(this, BaseApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_lipeishenqing, null)));


//        fragment_lipeishenqing_submit_bt = (Button) view.findViewById(R.id.fragment_lipeishenqing_submit_bt);
//
//        fragment_lipeishenqing_address_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_address_et);
//        fragment_lipeishenqing_idnum_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_idnum_et);
//        fragment_lipeishenqing_phone_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_phone_et);
//        fragment_lipeishenqing_people_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_people_et);
//
//        fragment_lipeishenqing_apply_type_tv = (TextView) view.findViewById(R.id.fragment_lipeishenqing_apply_type_tv);
//        fragment_lipeishenqing_conpany_tv = (TextView) view.findViewById(R.id.fragment_lipeishenqing_conpany_tv);
//
//        fragment_lipeishenqing_address_rl = (RelativeLayout) view.findViewById(R.id.fragment_lipeishenqing_address_rl);
//        fragment_lipeishenqing_carcode_rl = (RelativeLayout) view.findViewById(R.id.fragment_lipeishenqing_carcode_rl);
//
//        fragment_lipeishenqing_carcode_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_carcode_et);
//
//        fragment_lipeishenqing_bankNum_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_bankNum_et);
//        fragment_lipeishenqing_bankName_et = (EditText) view.findViewById(R.id.fragment_lipeishenqing_bankName_et);


        tpyeList.add("车险理赔申请");
        tpyeList.add("寿险理赔申请");
        fragment_lipeishenqing_apply_type_tv.setText(tpyeList.get(0));

        fragment_lipeishenqing_apply_type_tv.setOnClickListener(this);
        fragment_lipeishenqing_conpany_tv.setOnClickListener(this);
        fragment_lipeishenqing_submit_bt.setOnClickListener(this);


        getApiLiPeiSearch.type = PrefrenceConfig.ONE;
        getApiLiPeiSearch.execute(getActivity());

        return view;
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {


            case R.id.fragment_lipeishenqing_apply_type_tv:
                showPopWindow(view);
                return;

            case R.id.fragment_lipeishenqing_conpany_tv:
                showPopWindowCompany(view);
                return;

            case R.id.fragment_lipeishenqing_submit_bt:


                if (!BaseApplication.BasePreferences.readIsLogin()) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    return;
                }

                boolean applyType = fragment_lipeishenqing_apply_type_tv.getText().toString().equals("车险理赔申请");

                String cid = fragment_lipeishenqing_conpany_tv.getTag().toString().replaceAll(" ", "");
                String idnum = fragment_lipeishenqing_idnum_et.getText().toString().replaceAll(" ", "");
                String name = fragment_lipeishenqing_people_et.getText().toString().replaceAll(" ", "");
                String phone = fragment_lipeishenqing_phone_et.getText().toString().replaceAll(" ", "");
                String address = fragment_lipeishenqing_address_et.getText().toString().replaceAll(" ", "");
                String bank_name = fragment_lipeishenqing_bankName_et.getText().toString().replaceAll(" ", "");
                String bank_number = fragment_lipeishenqing_bankNum_et.getText().toString().replaceAll(" ", "");
                String license_plate = fragment_lipeishenqing_carcode_et.getText().toString().replaceAll(" ", "");


                if (TextUtils.isEmpty(name)) {

                    MyToast.showToast("姓名不能为空", getActivity());
                    return;

                }

                if (TextUtils.isEmpty(phone)) {

                    MyToast.showToast("手机号码不能为空", getActivity());
                    return;

                }

                if (!Validator.isMobile(phone)) {

                    MyToast.showToast("请输入正确手机号码", getActivity());
                    return;

                }

                if (TextUtils.isEmpty(idnum)) {

                    MyToast.showToast("身份证号不能为空", getActivity());
                    return;

                }

                if (!Validator.isIDCard(idnum)) {

                    MyToast.showToast("身份证号格式不正确", getActivity());
                    return;
                }

                if (TextUtils.isEmpty(address) && applyType) {

                    MyToast.showToast("事故地点不能为空", getActivity());
                    return;
                }

                if (TextUtils.isEmpty(license_plate) && applyType) {

                    MyToast.showToast("车牌号码不能为空", getActivity());
                    return;

                }

                if (TextUtils.isEmpty(bank_number)) {


                    MyToast.showToast("银行名称不能为空", getActivity());
                    return;

                }

                if (!Validator.isBankNum(bank_number)) {

                    MyToast.showToast("银行卡号不正确", getActivity());
                    return;
                }

                if (applyType) {


                    postApiClaims.cid = cid;
                    postApiClaims.tid = PrefrenceConfig.ONE;
                    postApiClaims.idnum = idnum;
                    postApiClaims.policynum = "";
                    postApiClaims.name = name;
                    postApiClaims.phone = phone;

                    postApiClaims.vehicle_license = "";
                    postApiClaims.address = address;
                    postApiClaims.bank_name = bank_name;
                    postApiClaims.bank_number = bank_number;
                    postApiClaims.license_plate = license_plate;

                    postApiClaims.userid = BaseApplication.BasePreferences.readUserId();
                    postApiClaims.execute(getActivity());


                } else {


                    postApiClaims.cid = cid;
                    postApiClaims.tid = PrefrenceConfig.TWO;
                    postApiClaims.idnum = idnum;
                    postApiClaims.policynum = "";
                    postApiClaims.name = name;
                    postApiClaims.phone = phone;


                    postApiClaims.vehicle_license = "";
                    postApiClaims.address = "";
                    postApiClaims.bank_name = bank_name;
                    postApiClaims.bank_number = bank_number;
                    postApiClaims.license_plate = "";


                    postApiClaims.userid = BaseApplication.BasePreferences.readUserId();
                    postApiClaims.execute(getActivity());

                }
                break;

        }

    }

    private void showPopWindow(View view) {

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_layout, null);

        ListView popList = ((ListView) contentView.findViewById(R.id.item_popList));

        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, tpyeList);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, fragment_lipeishenqing_conpany_tv.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));
        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fragment_lipeishenqing_apply_type_tv.setText(tpyeList.get(position));//TODO

                if (position == 0) {
                    getApiLiPeiSearch.type = PrefrenceConfig.ONE;
                    getApiLiPeiSearch.execute(getActivity());
                    fragment_lipeishenqing_address_rl.setVisibility(View.VISIBLE);
                    fragment_lipeishenqing_carcode_rl.setVisibility(View.VISIBLE);

                } else {
                    getApiLiPeiSearch.type = PrefrenceConfig.TWO;
                    getApiLiPeiSearch.execute(getActivity());
                    fragment_lipeishenqing_address_rl.setVisibility(View.GONE);
                    fragment_lipeishenqing_carcode_rl.setVisibility(View.GONE);
                }

                popupWindow.dismiss();
            }
        });
    }

    private void showPopWindowCompany(View view) {

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_layout, null);

        ListView popList = ((ListView) contentView.findViewById(R.id.item_popList));

        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, companyList);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, fragment_lipeishenqing_conpany_tv.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));
        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fragment_lipeishenqing_conpany_tv.setText(companyList.get(position));
                fragment_lipeishenqing_conpany_tv.setTag(mDataList.get(position).id);
                popupWindow.dismiss();
            }
        });
    }


}
